var pageUrl="/countryBack/business/findBusinessPage";
var findRetailingByIdUrl="/countryBack/business/findRetailingById";
/*var editUrl="/rest/phone/editPhone";
var updateUrl="/rest/phone/updatePhone";
var addUrl="/rest/phone/addPhone";
var deleteUrl="/rest/phone/deletePhone";*/
var pageSize=7;
var dataTable;
var employeeTable;
$(function(){
	  dataTable=$('#main').dataTable( {
		  "processing": true,
	      "serverSide": true,//列表的过滤,搜索和排序信息会传递到Server端进行处理,实现真翻页方案的必需属性.反之,所有的列表功能都在客户端计算并执行
	      "iDisplayLength" :pageSize,
	      "bRetrieve": true,
	      "aLengthMenu" : [7,20,50,100],
	      "bPaginate": true, //开关，是否显示分页器
	      "bLengthChange": true, //改变每页显示数据数量
	      "bFilter": false, //过滤功能
	      "bSort": false, //排序功能
	      "bInfo": true,//页脚信息
	      "bAutoWidth": true,//自动宽度
          "ajax": function (data, callback, settings){
        	//封装请求参数
        	  layer.load();
        	  var param = {};
        	  param.pageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
        	  param.page = (data.start / data.length)+1;//当前页码
        	  param.searchRetailingPlatform = $('#platform_select').val();//平台
        	  param.searchRetailingIndustry = $('#indestry_select').val();//行业
        	  param.searchRetailingRetailingName = $('#retailing_name').val();//网商名称
	       	 	$.ajax({
		      		url:pageUrl,
		      		type:'get',
		      		datatype:'json',
		      		data:param,
		      		success:function(result){
		      		  //封装返回数据
		      		    var returnData = {};
		      		    returnData.recordsTotal = result.total;//返回数据全部记录
		      		    returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
		      		    returnData.data = result.rows;//返回的数据列表
		      		    callback(returnData);
		      		    layer.closeAll();
		      		}
	       	 	});
          },
          "columns": [
			  {"orderable":false,"className":'text-c',"render":function(data,type,full){
				  return '<td><input name="" type="checkbox" value="'+full.id+'"></td>';
			  }},
			   { "data": "id","orderable":true,"className":'text-c'},
			   { "data": "siteId","orderable":false,"className":'text-c'},
              { "data": "retailingName","orderable":false,"className":'text-c',"render":function(data,type,full){
            	  if(data){
              	 	 return data;
              	  }
              	  return "";
                }},
              { "data": "brandsName","orderable":false,"className":'text-c',"render":function(data,type,full){
            	  if(data){
              	 	 return data;
              	  }
              	  return "";
                }},
              { "data": "retailingType","orderable":false,"className":'text-c',"render":function(data,type,full){
            	  if(data){
              	 	 return data;
              	  }
              	  return "";
                }},
                { "data": "companyName","orderable":false,"className":'text-c',"render":function(data,type,full){
                      if(data){
                         return data;
                      }
                      return "";
                    }},
			 { "data": "openTime","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},
			{ "data": "industry","orderable":false,"className":'text-c',"render":function(data,type,full){
              if(data){
                 return data;
              }
              return "";
            }},
            { "data": "category","orderable":false,"className":'text-c',"render":function(data,type,full){
              if(data){
                 return data;
              }
              return "";
            }},
            { "data": "commoditiesNumber","orderable":false,"className":'text-c',"render":function(data,type,full){
              if(data){
                 return data;
              }
              return "";
            }},
			 { "data": "retailingSite","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},
			{ "data": "expressDetailAddress","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},
			 { "data": "platform","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},
			 { "data": "isWechat","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},
			 { "data": "created","orderable":false,"className":'text-c',"render":function(data,type,full){
			  if(data){
				 return data;
			  }
			  return "";
			}},

              {"orderable":false,"className":'text-c',"render":function(data,type,full){
            	  return "<a  onclick='detail(\""+full.id+"\");'>详情</a>";
              }}
          ]
      } ).api();

});
function bathDelete(){
	var ids=[];
	$("#main input:checked").each(function(i){
		if($(this).val()!=''){
			ids.push($(this).val());
		}
	});
	if(ids.length==0){
		layer.msg("至少选择一行进行操作!");
		return;
	}
	layer.confirm('你真的要删除勾选的订单吗?', function(index){
		layer.load();
		layer.close(index);
		 $.ajax({
			url:deleteUrl,
			type:'get',
			datatype:'json',
			data:{"ids":ids.join(",")},
			success:function(result){
				var isSuccess=result.isSuccess;
				if(isSuccess){
					searchData();
				}else{
					layer.msg(result.msg);
				}
			}
		});
	});
};
function searchData()
{
	dataTable.ajax.reload(null,false);
};
function clearFrom(){
	$('#submitForm')[0].reset();
};
function detail(id){
	//clearFrom();
//	layer.load();
	//var form = $("#submitForm");
	//form.attr("action",updateUrl);
	$.ajax({
  		url:findRetailingByIdUrl,
  		type:'get',
  		datatype:'json',
  		data:{"retailingId":id},
  		success:function(result){
  			layer.closeAll();
  			$('#formShow').show();
  			layer.open({
  				  type: 1,
  				  area: ['80%','60%'], //宽高s
  				  content: $('#formShow')
  				});
  			var data = result.data;
  			for(var d in data){
  				var $obj=$("#"+d);
  				if($obj.length>0){
  				$obj.text(data[d]+"");
	  				/*var tagName=$obj[0].tagName;
	  				if(tagName=="IMG"){
	  					$obj.attr("src",""+data[d]+"");
	  				}else{
	  					$obj.val(data[d]+"");
	  				}*/
  				}
  				//员工信息
  				if(d=="employeeList" && data[d]!=null){
  				//刪除除第一行以外的行，第一行用于复制
               $("#employeeTable tr:gt(0):not(:eq(0))").remove();
  				$.each(data[d], function (i, n) {
                        var row = $("#template").clone();
                        row.find("#relName").text(n.relName);
                        debugger;
                        row.find("#age").text(n.age);
                        row.find("#educationLevel").text(n.educationLevel);
                        row.find("#employeeTypeName").text(n.employeeTypeName);
                        row.appendTo("#employeeTable");//添加到模板的容器中
                    });
                     //删除第一行
                     $("#employeeTable tr:gt(0):eq(0)").remove();
                   /* //表格存在，先销毁再创建
                    if(employeeTable!=null){
                        employeeTable.fnClearTable();
                        employeeTable.fnDestroy();
                    }
                     employeeTable =   $("#employeeTable").dataTable({
                                             "bPaginate": false, //开关，是否显示分页器
                                             "bAutoWidth": true,//自动宽度
                                             "bInfo": false,//页脚信息
                                             "bFilter":false,//过滤功能，不显示默认的搜索框
                         				        data:data[d],
                         				         columns: [
                                                       { data: 'relName',"orderable":false},
                                                       { data: 'age' ,"orderable":false},
                                                       { data: 'educationLevel',"orderable":false },
                                                       { data: 'employeeTypeName' ,"orderable":false}
                                                   ]
                         				    });
*/
  				}
  			}

  		}
	});
};

function submitFrom(){
     var form = $("#submitForm");
     var url=form.attr("action");
     var name=$("#name").val();
     var company=$("#company").val();
     var telephone=$("#telephone").val();
//      if(!name){
//     	 layer.msg("联系人不能为空!");
//     	 return;
//      }
//      if(!company){
//     	 layer.msg("单位不能为空!");
//     	 return;
//      }
//      if(!telephone){
//     	 layer.msg("电话不能为空!");
//     	 return;
//      }
     layer.load();
     var options  = {
        type:'post',
        success:function(data)
        {
       	 	layer.closeAll();
       		var isSuccess=data.isSuccess;
       		if(isSuccess){
       			searchData();
       		}else{
       			layer.msg(data.msg);
       		}
        }
    };
    form.ajaxSubmit(options);
};
function add(){
	clearFrom();
	var form = $("#submitForm");
	form.attr("action",addUrl);
	$('#formShow').show();
			layer.open({
				  type: 1,
				  area: ['80%','60%'], //宽高s
				  content: $('#formShow')
				});
// 	layer.load();
// 	$.ajax({
//   		url:editUrl,
//   		type:'get',
//   		datatype:'json',
//   		data:{"id":id},
//   		success:function(result){
//   			layer.closeAll();
//   			$('#formShow').show();
//   			layer.open({
//   				  type: 1,
//   				  area: ['80%','60%'], //宽高s
//   				  content: $('#formShow')
//   				});
//   			for(var d in result){
//   				var $obj=$("#"+d);
//   				if($obj.length>0){
// 	  				var tagName=$obj[0].tagName;
// 	  				if(tagName=="IMG"){
// 	  					$obj.attr("src",""+result[d]+"");
// 	  				}else{
// 	  					$obj.val(result[d]+"");
// 	  				}
//   				}
//   			}

//   		}
// 	});
};